package org.social;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.constant.ActivityConstant;
import org.social.db.DbSave;
import org.social.db.bean.Activity;
import org.social.db.bean.ActivityEntry;
import org.social.db.dao.ActivityEntryDaoImpl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.ContextManager;
import com.sojoys.artifact.manager.PropManager;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws Exception {
		// 设置使用的配置文件
		PropManager.use("app.properties");
		// SpringContext初始化
		ContextManager.me().init("app-init.xml");
//		DbSave.init();
//		Date nowDate = new Date();
//		// 创建签到活动
//		createSignActivity(nowDate);
//		// 创建一个7日登陆活动
//		createSevenActivity(nowDate);
//		// 创建一个次日登陆活动
//		createTwoActivity(nowDate);
//		// 体力活动
//		createApSupply(nowDate);
		
		List<ActivityEntry> all = ActivityEntryDaoImpl.me().getAll();
		for (ActivityEntry activityEntry : all) {
			activityEntry.setExtend(Maps.newHashMap());
			ActivityEntryDaoImpl.me().update(activityEntry);
		}
		
		DbSave.saveAll();
		Thread.sleep(5000);
		System.exit(0);
	}

	private static void createApSupply(Date nowDate) {
		JSONObject json = new JSONObject();

		Map finishParams = Maps.newHashMap();
		json.put("title", "体力补给");
		Activity activity = new Activity(0, 0, true, null, null,new Date(), ActivityConstant.ACTIVITY_TYPE_AP_SUPPLY,
				ActivityConstant.RESET_TYPE_DAY, ActivityConstant.ACTIVITY_DATA_TYPE_SERVER, json.toJavaObject(Map.class), 4,0);
		activity = activity.insert();

		Map reward = Maps.newHashMap();

		int progress = 0;

		reward.put(500004, 30);
		json = new JSONObject();
		json.put("title", "上午");
		json.put(ActivityConstant.ENTRY_TRIGGER_TIME_TYPE, ActivityConstant.ENTRY_TRIGGER_TIME_DAY);
		json.put(ActivityConstant.ENTRY_TRIGGER_BEGIN_TIME, 4320);
		json.put(ActivityConstant.ENTRY_TRIGGER_END_TIME, 4440);
		
		
		ActivityEntry entry = new ActivityEntry(0, activity.getId(), true, 0, Maps.newHashMap(), progress, reward,
				Maps.newHashMap(),json.toJavaObject(Map.class), 1);
		entry.insert();
		reward.clear();

		reward.put(500004, 50);
		json = new JSONObject();
		json.put("title", "下午");
		json.put(ActivityConstant.ENTRY_TRIGGER_TIME_TYPE, ActivityConstant.ENTRY_TRIGGER_TIME_DAY);
		json.put(ActivityConstant.ENTRY_TRIGGER_BEGIN_TIME, 6480);
		json.put(ActivityConstant.ENTRY_TRIGGER_END_TIME, 6600);
		entry = new ActivityEntry(0, activity.getId(), true, 0, Maps.newHashMap(), progress, reward,
				Maps.newHashMap(),json.toJavaObject(Map.class), 2);
		entry.insert();
		reward.clear();
	}

	private static void createTwoActivity(Date nowDate) {
		JSONObject json = new JSONObject();

		Map finishParams = Maps.newHashMap();
		json.put("title", "次日登陆");
		Activity activity = new Activity(0, 0, true, null, null,new Date(), ActivityConstant.ACTIVITY_TYPE_MORROW_LOGIN,
				ActivityConstant.RESET_TYPE_NO, ActivityConstant.ACTIVITY_DATA_TYPE_PLAYER, json.toJavaObject(Map.class), 3,0);
		activity = activity.insert();

		Map reward = Maps.newHashMap();

		reward.put(500001, 2);
		json = new JSONObject();
		json.put("title", "登陆");
		int progress = 2;
		ActivityEntry entry = new ActivityEntry(0, activity.getId(), true, 3001, Maps.newHashMap(), progress, reward,
				Maps.newHashMap(),json.toJavaObject(Map.class), 1);
		entry.insert();
		reward.clear();
	}

	private static void createSevenActivity(Date nowDate) {
		JSONObject json = new JSONObject();
		Map reward = Maps.newHashMap();
		Map finishParams = Maps.newHashMap();

		json.put("title", "七日登陆");
		Activity activity = new Activity(0, 0, true, null, null,new Date(), ActivityConstant.ACTIVITY_TYPE_ELEVEN_LOGIN,
				ActivityConstant.RESET_TYPE_NO, ActivityConstant.ACTIVITY_DATA_TYPE_PLAYER, json.toJavaObject(Map.class), 2,0);
		activity = activity.insert();

		reward = Maps.newHashMap();
		for (int i = 1; i <= 7; i++) {
			reward.put(500001, i);
			json = new JSONObject();
			json.put("title", "第" + i + "天");
			ActivityEntry entry = new ActivityEntry(0, activity.getId(), true, 3001, Maps.newHashMap(), i, reward,
					Maps.newHashMap(),json.toJavaObject(Map.class), i);
			entry.insert();
			reward.clear();
		}
	}

	/**
	 * 创建签到活动
	 * 
	 * @param nowDate
	 * @return
	 */
	private static Map createSignActivity(Date nowDate) {
		// 创建一个签到活动
		JSONObject json = new JSONObject();
		json.put("title", "每日签到");
		Activity activity = new Activity(0, 0, true, null, null,new Date(), ActivityConstant.ACTIVITY_TYPE_MONTH_SIGN,
				ActivityConstant.RESET_TYPE_MONTH, ActivityConstant.ACTIVITY_DATA_TYPE_PLAYER, json.toJavaObject(Map.class), 1,0);
		activity = activity.insert();

		Map reward = Maps.newHashMap();
		Map finishParams = Maps.newHashMap();
		ActivityEntry nextEntry = null;
		for (int i = 1; i <= 62; i++) {
			// 奇数月
			int group = 0;
			if (i <= 31) {
				reward.put(500001, i);
				group = 1;

			} else {
				// 偶数月
				reward.put(500002, i - 31);
				group = 2;
			}

			if (nextEntry != null) {
				finishParams.put("pre_entry_id", nextEntry.getId());
			}
			json = new JSONObject();
			String title = group == 1 ? "第" + i + "天" : "第" + (i - 31) + "天";
			json.put("title", title);
			json.put("group", group);

			int progress = group == 1 ? i : i - 31;
			int rank = group == 1 ? i : i - 31;
			ActivityEntry entry = new ActivityEntry(0, activity.getId(), true, 3001, finishParams, progress, reward,
					Maps.newHashMap(),json.toJavaObject(Map.class), rank);
			entry.insert();
			if (i == 31) {
				nextEntry = null;
			} else {
				nextEntry = entry;
			}

			finishParams.clear();
			reward.clear();
		}
		return reward;
	}
}
